<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="账号">
        <el-input v-model="searchForm.username" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="searchForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="searchForm.accountName" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>  
    </el-form>
  
    <el-table :data="tableData" border style="width: 100%" :row-class-name="tableRowClassName">
        <el-table-column prop="username" label="账号" width="120" align="center">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="120" align="center">
        </el-table-column>
        <el-table-column prop="accountName" label="昵称" width="120" align="center">
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="80" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="160" align="center">
        </el-table-column>
        <el-table-column prop="idNumber" label="身份证号" width="260" align="center">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="200" align="center">
        </el-table-column>
        <el-table-column prop="createTime" label="注册日期" width="200" align="center">
        </el-table-column>
        <el-table-column prop="signature" label="个性签名" width="200" align="center">
        </el-table-column>
        <el-table-column prop="bnumber" label="借阅次数" width="80" align="center">
        </el-table-column>
        <el-table-column prop="returnNum" label="还书次数" width="80" align="center">
        </el-table-column>
        <el-table-column label="账号状态" fixed="right" align="center">
          <template slot-scope="scope">
              {{scope.row.isFrozen == '1' ? '冻结' : '正常' }}
          </template>
        </el-table-column> 
        <el-table-column label="操作" width="160" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button :disabled="scope.row.isFrozen == '1' ? false : true" size="mini" @click="infrozen(scope.row.id)">解冻</el-button>
            <el-button :disabled="scope.row.isFrozen == '1' ? true : false" type="danger" size="mini" @click="frozen(scope.row.id)">冻结</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <el-pagination background layout="total,sizes,prev,pager,next,jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :total="total"
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="[5,10,15,20,30,40,50]"></el-pagination>
  </div>
</template>

<script>
import axios from 'axios';
export default {
    data() {
      return {
        searchForm:{
            username:'',
            name:'',
            accountName:''
        },
        tableData:[{
          username:'qwer',
            name:"qwer",
            accountName:"qwer",
            sex:"nan",
            phone:"qwer",
            idNumber:"qwer",
            email:"qwer",
            createTime:'123',
            signature:"123",
            bNumber:"1",
            pNumber:"1",
        }],
        total:0,
        currentPage:1,
        pageSize:5,
      }
    },
    methods: {
      /*  控制表格颜色 */
      tableRowClassName({row}) {
        if (row.isFrozen == "1") {
          return 'warning-row';
        } /* else if (row.isFrozen == 1) {
          return 'success-row';
        } */
        return '';
      },

      /* 获取用户数据 */
      getuserlist(){
        axios.get(this.url + "/user/list",{
        params:{
          "page":this.currentPage,
          "pageSize":this.pageSize,
          "username":this.searchForm.username,
          "name":this.searchForm.name,
          "accountName":this.searchForm.accountName,
          "isFrozen":this.isFrozen
        }
      }).then((res) => {
        if(res.data.code == 1){
          this.total = parseInt(res.data.data.total)
          this.tableData = res.data.data.records

        }else{
          this.$message.error("信息加载出错")
        }
      }).catch(() => {
        this.$message.error("信息加载出错")
      });
      },
      infrozen(val){
        axios.put(this.url + "/user/update",
          {
            "id":val,
            "isFrozen":0
          }
        ).then((res) => {
                if(res.data.code == 1){
                    this.$message.success("解冻成功")
                    this.getuserlist()                    
                }else{
                    this.$message.error(res.data.msg)                    
                }
            }).catch(() => {
                this.$message.error("操作失败")    
        });
      },
      frozen(val){
        axios.put(this.url + "/user/update",
          {
            "id":val,
            "isFrozen":1
          }
        ).then((res) => {
                if(res.data.code == 1){
                    this.$message.success("冻结成功")
                    this.getuserlist()                       
                }else{
                    this.$message.error(res.data.msg)                    
                }
            }).catch(() => {
                this.$message.error("操作失败")    
        });
      },
      search() {
        this.getuserlist()   
      },
      handleSizeChange:function(val){
        this.pageSize = val
        axios.get(this.url + "/user/list",{
        params:{
          "page":this.currentPage,
          "pageSize":val,
          "username":this.searchForm.username,
          "name":this.searchForm.name,
          "accountName":this.searchForm.accountName,
        }
      }).then((res) => {
        if(res.data.code == 1){
          this.total = parseInt(res.data.data.total)
          this.tableData = res.data.data.records

        }else{
          this.$message.error("信息加载出错")
        }
      }).catch(() => {
        this.$message.error("信息加载出错")
      });
      },
      handleCurrentChange:function(val){
        this.currentPage = val
        axios.get(this.url + "/user/list",{
        params:{
          "page":val,
          "pageSize":this.pageSize,
          "username":this.searchForm.username,
          "name":this.searchForm.name,
          "accountName":this.searchForm.accountName,
        }
      }).then((res) => {
        if(res.data.code == 1){
          this.total = parseInt(res.data.data.total)
          this.tableData = res.data.data.records

        }else{
          this.$message.error("信息加载出错")
        }
      }).catch(() => {
        this.$message.error("信息加载出错")
      });
      },
    },
    mounted () {
      axios.get(this.url + "/user/list",{
        params:{
          "page":this.currentPage,
          "pageSize":this.pageSize
        }
      }).then((res) => {
        if(res.data.code == 1){
          this.total = parseInt(res.data.data.total)
          this.tableData = res.data.data.records

        }else{
          this.$message.error("信息加载出错")
        }
      }).catch(() => {
        this.$message.error("信息加载出错")
      });
    
    }
}
</script>

<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #ffff;
  }
</style>